package com.systop.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.systop.pojo.Comment;
import com.systop.pojo.Goods;
import com.systop.redis.RedisUntil;
import com.systop.service.CommentService;
import com.systop.unti.PageQO;
import com.systop.unti.PageVO;
import com.systop.unti.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;
    @Autowired
    private RedisUntil redisUntil;
    //评论
    @PostMapping(value = "/addComment")
    public ResponseResult addComment(@RequestParam("comment_goods_id") Integer comment_goods_id,@RequestParam("comment_user_id") Integer comment_user_id,
                                     @RequestParam("comment_content") String comment_content,@RequestParam("comment_times") String comment_times){
        Comment comment = new Comment();
        comment.setComment_goods_id(comment_goods_id);
        comment.setComment_user_id(comment_user_id);
        comment.setComment_content(comment_content);
        comment.setComment_times(comment_times);
        String c_u_id = comment_user_id.toString();
        redisUntil.hset("goodsComments",c_u_id,comment);
        return commentService.addComment(comment);
    }
    //根据商品id查看评论
    @PostMapping(value = "/selCommentByGoodsId")
    public PageVO<Comment> selCommentByid(PageQO pageQO,@RequestParam("comment_goods_id") Integer comment_goods_id){
        Page<Comment> page = PageHelper.startPage(pageQO.getPageNum(), pageQO.getPageSize(), pageQO.getOrderBy());
        commentService.selCommentByid(comment_goods_id);
        System.out.println();
        return PageVO.build(page);
    }
    //用户查看自己给商品的评论（根据用户id查询消息）
    @PostMapping(value = "/selCommentByUserId")
    public PageVO<Comment> selCommentByUserid(PageQO pageQO,@RequestParam("comment_user_id") Integer comment_user_id){
        Page<Comment> page = PageHelper.startPage(pageQO.getPageNum(), pageQO.getPageSize(), pageQO.getOrderBy());
        commentService.selCommentByUserId(comment_user_id);
        return PageVO.build(page);
    }
    //本人查看自己商品的评论
//    @PostMapping(value = "/test")
//    public List test(@RequestParam("user_id") Integer user_id, @RequestParam("comment_goods_id") Integer comment_goods_id){
//        Comment comment = new Comment();
//        List list1 = new ArrayList<>(comment);
//        Map<Comment,Goods> list = commentService.selCommentAndGoods(comment_goods_id);
//        System.out.println(user_id);
//        System.out.println(list);
//        return list;
//    }
}
